package com.mmc.tianmu.im.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mmc.tianmu.im.entity.ChatMessage;
import com.mmc.tianmu.im.util.JsonUtil;
import com.mmc.tianmu.im.util.RedisListenerBean;

/** 
* @author 作者 geDuo
* @version 创建时间：2021年8月6日 下午3:49:58 
* @explain 类说明 
*/
@RestController
@RequestMapping("/message/")
public class MessageController {
	private static final Logger LOGGER = LoggerFactory.getLogger(RedisListenerBean.class);

	@Value("${redis.channel.msgToAll}")
	private String msgToAll;

	@Autowired
	private RedisTemplate<String, String> redisTemplate;

	/**
	 * 发送消息
	 * 
	 * @param chatMessage
	 */
	@PostMapping("sendMessage")
	public void sendMessage(@RequestBody ChatMessage chatMessage) {
		try {
			// 发送消息-保存在redis-由redis订阅分发
			redisTemplate.convertAndSend(msgToAll, JsonUtil.parseObjToJson(chatMessage));
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
	}
}
